﻿using AutoMapper;
using HealthcareSystem.API.Write.Application.Command;
using HealthcareSystem.Domain;
using MediatR;
using WeekExam.PassPort.ErrorCode;
using WeekExam.PassPort.Infrastructure.Interface;

namespace HealthcareSystem.API.Write.Application.CommandHandel
{
    /// <summary>
    /// 居民档案处理方法
    /// </summary>
    public class FilingCommandHand : IRequestHandler<FilingCommand, ApiResult<int>>
    {
        /// <summary>
        /// 居民档案仓储
        /// </summary>
        IBaseRepository<Record> recordRep;
        /// <summary>
        /// 居民体格仓储
        /// </summary>
        IBaseRepository<Physique> phyRep;
        /// <summary>
        /// 居民行为仓储
        /// </summary>
        IBaseRepository<Behavior> behaRep;
        /// <summary>
        /// AUtomapping
        /// </summary>
        IMapper mapper;

        /// <summary>
        /// 居民档案
        /// </summary>
        /// <param name="recordRep">居民档案仓储</param>
        /// <param name="phyRep">居民体格仓储</param>
        /// <param name="behaRep">居民行为仓储</param>
        /// <param name="mapper">AUtomapping</param>
        public FilingCommandHand(IBaseRepository<Record> recordRep, IBaseRepository<Physique> phyRep, IBaseRepository<Behavior> behaRep, IMapper mapper)
        {
            this.recordRep = recordRep;
            this.phyRep = phyRep;
            this.behaRep = behaRep;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">方法</param>
        /// <returns>返回方法</returns>
        public async Task<ApiResult<int>> Handle(FilingCommand request, CancellationToken cancellationToken)
        {
           var meo=mapper.Map<Record>(request);
           var mei=mapper.Map<Physique>(request);
           var meu=mapper.Map<Behavior>(request);
            await recordRep.AddAsync(meo);
            await phyRep.AddAsync(mei);
            await behaRep.AddAsync(meu);

            return new ApiResult<int>
            {
                Code = ResultCode.Success,
                Data = 1,
                Message = "添加成功"
            };
        }
    }
}
